package com.xcdh.ittools.controller;

import com.api.ittools.entity.ITToolPage;
import com.api.ittools.entity.ToolDatasource;
import com.api.ittools.vo.ToolDatasourceVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xcdh.commons.controller.BaseController;
import com.xcdh.domain.RequestPage;
import com.xcdh.domain.vo.PageVO;
import com.xcdh.domain.vo.R;
import com.xcdh.domain.vo.TypeValueA2B;
import com.xcdh.ittools.service.ToolDatasourceService;
import com.xcdh.orm.client.DsClient;
import com.xcdh.orm.entity.OrmDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/toolDatasource")
public class ToolDatasourceController extends BaseController<ToolDatasource , ToolDatasourceService> {

    @Autowired
    private ToolDatasourceService toolDatasourceService ;

    @Autowired
    private DsClient dsClient ;

    @Override
    public R<PageVO<ToolDatasource>> listByPage(@RequestBody RequestPage<ToolDatasource> page) {
        IPage<ToolDatasourceVo> voIPage = toolDatasourceService.listByPage(page);
        List<ToolDatasource> rows = voIPage.getRecords().stream().map(ds -> {
            ds.setPassword(null);
            return TypeValueA2B.a2b(ds, ToolDatasource.class);
        }).collect(Collectors.toList());
        return R.page(voIPage.getTotal(),rows);
    }
    @PostMapping("/findListDataSources")
    public R<List<OrmDataSource>> findListDataSources(OrmDataSource dataSource) {
        return dsClient.findListDataSources(dataSource) ;
    }

    @PostMapping("/queryOrmDataSource")
    public R<PageVO<OrmDataSource>> queryOrmDataSource(@RequestBody RequestPage<OrmDataSource> page) {
        R<PageVO<OrmDataSource>> voIPage = dsClient.findDataSources(page);
        PageVO<OrmDataSource> result = voIPage.getData();
        for (OrmDataSource datum : result.getRows()) {
            datum.setPassword(null);
        }
        return R.page(result.getTotal(),result.getRows());
    }



    @PostMapping("/test")
    public R test(){
        System.out.println(toolDatasourceService);
        return R.ok() ;
    }
}
